package com.lushu.chapitre1.states {
import com.lushu.chapitre1.core.Configuration;
import com.lushu.chapitre1.engine.GameEngine;
import com.lushu.chapitre1.graphics.controls.LoadingBar;
import com.lushu.chapitre1.graphics.elements.UserAvatar;
import com.sfdk.core.ConfigReader;
import com.sfdk.core.DataStore;
import com.sfdk.core.GameProxy;
import com.sfdk.core.abstract.AbstractState;
import com.sfdk.events.ConfigEvent;
import com.sfdk.events.IOEvent;
import com.sfdk.events.ResourcesEvent;
import com.sfdk.graphics.core.MovieClipEx;
import com.sfdk.graphics.core.SpriteEx;
import com.sfdk.resources.ResourcesLoader;

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.Event;
import flash.utils.ByteArray;
import flash.utils.getDefinitionByName;

public final class LoadDataState extends AbstractState {
    private var _configReader:ConfigReader;
    private var _resourcesLoader:ResourcesLoader;

    private var _loadingScreen:Sprite;
    private var _loadingBar:LoadingBar;
    //private var _loginDialog:LoginBox;

    private var _userAvatar:UserAvatar;

    public function LoadDataState(gameProxy:GameProxy, parameters:Object = null) {
        super(gameProxy, new MovieClipEx(), parameters);
    }

    /** State Mechanism **/

    protected override function initState(parameters : Object = null):void {
        super.initState();

        _loadingScreen = new Sprite();
        _view.addChild(_loadingScreen);

        _loadingBar = new LoadingBar();
        _view.attachChild(_loadingBar, (672 - 125) >> 1, 558 - 5 - 12 - 5, "loadingBar");

        _configReader = new ConfigReader();
        _configReader.addEventListener(ConfigEvent.CONFIG_READ, onConfigLoaded, false, 0, true);
    }

    public override function endState():void {
        super.endState();
        _configReader = null;
    }

    /** Event Handlers **/

    private function onConfigLoaded(e:ConfigEvent):void {
        var configuration:Configuration = new Configuration(_configReader);
        DataStore.store("configuration", configuration);

        _resourcesLoader = new ResourcesLoader(_loadingBar);
        _resourcesLoader.addEventListener(ResourcesEvent.SPLASHSCREEN_LOADED, onSplashScreenLoaded, false, 0, true);
        _resourcesLoader.addEventListener(ResourcesEvent.RESOURCES_LOADED, onAllResourcesLoaded, false, 0, true);

        //_resourcesLoader.addEventListener(RemoteEvent.DESCRIPTION_OK, prepareLogin, false, 0, true);
    }

    private function onSplashScreenLoaded(e:Event):void {

    }

    private var _userView:Bitmap;
    private var _gameEngine:GameEngine;

    private function onAllResourcesLoaded(e:Event):void {
        _view.removeChild(_loadingBar);
        _loadingBar = null;
        _resourcesLoader = null;

//			_userAvatar = new UserAvatar();
//			_userView = new Bitmap(_userAvatar.getView());
//			_gameProxy.addChild(_userView);

        _gameEngine = new GameEngine(_view);
        //_gameEngine.addElement(_mapElementList);
        //displayLoginWindow();
    }

//    private function displayLoginWindow():void {
//        _loginDialog = new LoginBox();
//        _loginDialog.onValidation = onLogin;
//        _view.attachChild(_loginDialog, 460, 230, "loginBox");
//    }
//
//    private var _connectClient:SGSClient;
//
//    private function onLogin(e:Event):void {
//        var configuration:Configuration = DataStore.get("configuration") as Configuration;
//        var serverKeyPair:ServerKeyPair = configuration.currentServer;
//        _connectClient = new SGSClient(serverKeyPair.serverAddress, serverKeyPair.serverPort);
//        DataStore.store("serverManager", _connectClient);
//        _connectClient.addEventListener(SGSEvent.LOGIN_SUCCESS, onLoginSuccess, false, 0, true);
//        _connectClient.addEventListener(SGSEvent.LOGIN_FAILURE, onLoginFailure, false, 0, true);
//        _connectClient.addEventListener(SGSEvent.SESSION_MESSAGE, onSessionMessage, false, 0, true);
//        _connectClient.addEventListener(SGSEvent.CHANNEL_MESSAGE, onChannelMessage, false, 0, true);
//        _connectClient.login(_loginDialog.getLoginValue(), _loginDialog.getPasswordValue());
//    }
//
//    private function onLoginSuccess(loginEvent:SGSEvent):void {
//
//    }
//
//    private function onLoginFailure(loginEvent:SGSEvent):void {
//        var messageDialog:MessageDialog = new MessageBox();
//        messageDialog.init({messageTitle : MessageBundleKey.GENERIC_ERROR, messageContent : MessageBundleKey.ACCOUNT_USER_PASS_ERROR});
//        _gameProxy.attachChild(messageDialog, 300, 300, "errorMessage");
//    }
//
//    private function onSessionMessage(messageEvent:SGSEvent):void {
//        var message:SGSMessage = messageEvent.sessionMessage.readObject();
//        if (message is SGSMessage) {
//            if (message.messageType.equals(SGSMessageType.MAP_INFORMATION)) {
//                var idPair:IDTypePair;
//                for each(idPair in message.data) {
//                    if (idPair.graphicalType == "Poussin") {
//                        _gameEngine.addElement([idPair]);
//                    }
//                }
//            }
//        }
//    }
//
//    private function onChannelMessage(messageEvent:SGSEvent):void {
//        var message:SGSMessage = messageEvent.channelMessage.readObject();
//        if (message is SGSMessage) {
//            if (message.messageType.equals(SGSMessageType.PLAYER_JOINED)) {
//                var message:SGSMessage = new SGSMessage(SGSMessageType.CREATE_CURRENT_CLIENT_CHARACTER, ["Kanegank"]);
//                var byteArray:ByteArray = new ByteArray();
//                byteArray.writeObject(message);
//                _connectClient.sessionSend(byteArray);
//
//                var message:SGSMessage = new SGSMessage(SGSMessageType.GET_MAP_INFO);
//                var byteArray:ByteArray = new ByteArray();
//                byteArray.writeObject(message);
//                _connectClient.sessionSend(byteArray);
//            }
//            else if (message.messageType.equals(SGSMessageType.MAP_INFORMATION)) {
//                trace(message.data);
//            }
//            else if (message.messageType.equals(SGSMessageType.UPDATE_BOARD_SPACES)) {
//                var data:Array = message.data;
//                var boardSpace:BoardSpace;
//                var identifiers:Array;
//                for each(boardSpace in data) {
//                    identifiers = boardSpace.identifiers;
//                    trace(identifiers)
//                    if (identifiers.length == 2) {
//                        if (_gameEngine != null)
//                            _gameEngine.movePoussin(identifiers[1], boardSpace.x, boardSpace.y);
//                    }
//                }
//                var message:SGSMessage = new SGSMessage(SGSMessageType.GET_MAP_INFO);
//                var byteArray:ByteArray = new ByteArray();
//                byteArray.writeObject(message);
//                _connectClient.sessionSend(byteArray);
//            }
//            else {
//                var message:SGSMessage = new SGSMessage(SGSMessageType.MOVE_CLIENT_TO_GAME, ["Handonimi","Kanegank"]);
//                var byteArray:ByteArray = new ByteArray();
//                byteArray.writeObject(message);
//                _connectClient.sessionSend(byteArray);
//            }
//        }
//    }
}
}